package org.jetlinks.pro.datasource.rdb;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.rdb.executor.jdbc.JdbcReactiveSqlExecutor;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@AllArgsConstructor
@Slf4j
public class RDBJdbcReactiveSqlExecutor extends JdbcReactiveSqlExecutor {
    private final DataSource dataSource;

    @Override
    public Mono<Connection> getConnection() {
        return Mono
            .using(dataSource::getConnection,
                Mono::just,
                source -> {
                    try {
                        source.close();
                    } catch (SQLException e) {
                        log.error(e.getMessage(), e);
                    }
                },
                false
            )
            .publishOn(Schedulers.boundedElastic());
    }
}
